/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OrderPanel.java
 *
 * Created on Mar 3, 2010, 3:20:00 PM
 */

package dat215projekt.gui.components;

import dat215projekt.data.IMatDataHandlerExtended;
import java.awt.Color;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import javax.swing.DefaultListModel;
import javax.swing.border.TitledBorder;
import se.chalmers.cse.dat215.project.Order;
import se.chalmers.cse.dat215.project.ShoppingItem;

/**
 *
 * @author ludwigk
 */
public class OrderPanel extends javax.swing.JPanel {

    private static final NumberFormat moneyFormat;
    static {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        symbols.setGroupingSeparator(' ');

        moneyFormat = new DecimalFormat("###,##0.00 kr",symbols);
    }

    private static final DateFormat dateFormat = new SimpleDateFormat("kk:mm:ss d/M  yyyy");
    private Order order;

    private static final Color rowStripeColor_1 = new Color(240,245,250);
    private static final Color rowStripeColor_2 = new Color(255,255,255);

    /** Creates new form OrderPanel */
    public OrderPanel(Order o, int num) {
        initComponents();

        this.order = o;

        // Total amount
        double totalAmount = 0;
        DefaultListModel model = new DefaultListModel();

        for(ShoppingItem i : o.getItems()){
            totalAmount += i.getTotal();
            model.addElement(i.getAmount()+" "+i.getProduct().getName());
        }

        productList.setModel(model);

        priceLabel.setText(moneyFormat.format(totalAmount));


        ((TitledBorder)boredPanel.getBorder()).setTitle(dateFormat.format(o.getDate())+" - Ordernummer: "+o.getOrderNumber());

        amountLabel.setText(o.getItems().size()+" st");


         // Set background color for row-striping
        if(num % 2 == 1){
            boredPanel.setBackground(rowStripeColor_1);
        }
        else{
             boredPanel.setBackground(rowStripeColor_2);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        boredPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        productList = new javax.swing.JList();
        addCartButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        amountLabel = new javax.swing.JLabel();
        priceLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dat215projekt.Dat215projektApp.class).getContext().getResourceMap(OrderPanel.class);
        boredPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("boredPanel.border.title"))); // NOI18N
        boredPanel.setMaximumSize(new java.awt.Dimension(32767, 162));
        boredPanel.setName("boredPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        productList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        productList.setName("productList"); // NOI18N
        jScrollPane1.setViewportView(productList);

        addCartButton.setText(resourceMap.getString("addCartButton.text")); // NOI18N
        addCartButton.setToolTipText(resourceMap.getString("addCartButton.toolTipText")); // NOI18N
        addCartButton.setName("addCartButton"); // NOI18N
        addCartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCartButtonActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        amountLabel.setText(resourceMap.getString("amountLabel.text")); // NOI18N
        amountLabel.setName("amountLabel"); // NOI18N

        priceLabel.setText(resourceMap.getString("priceLabel.text")); // NOI18N
        priceLabel.setName("priceLabel"); // NOI18N

        javax.swing.GroupLayout boredPanelLayout = new javax.swing.GroupLayout(boredPanel);
        boredPanel.setLayout(boredPanelLayout);
        boredPanelLayout.setHorizontalGroup(
            boredPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(boredPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 125, Short.MAX_VALUE)
                .addGroup(boredPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addCartButton)
                    .addGroup(boredPanelLayout.createSequentialGroup()
                        .addGroup(boredPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(boredPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(priceLabel)
                            .addComponent(amountLabel))))
                .addContainerGap())
        );
        boredPanelLayout.setVerticalGroup(
            boredPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(boredPanelLayout.createSequentialGroup()
                .addGroup(boredPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(boredPanelLayout.createSequentialGroup()
                        .addGroup(boredPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(boredPanelLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2))
                            .addGroup(boredPanelLayout.createSequentialGroup()
                                .addComponent(amountLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(priceLabel)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                        .addComponent(addCartButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(boredPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(boredPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addCartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCartButtonActionPerformed
        for(ShoppingItem i : order.getItems())
            IMatDataHandlerExtended.getInstance().addToShoppingCart(i.getProduct(), (int)i.getAmount());
    }//GEN-LAST:event_addCartButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCartButton;
    private javax.swing.JLabel amountLabel;
    private javax.swing.JPanel boredPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JList productList;
    // End of variables declaration//GEN-END:variables

}
